# -*- coding: utf-8 -*-

import os
import re

from collections import Counter


def calc_counter(file_name):
    """统计一个英文文本中单词出现的次数"""
    pattern = r"""[A-Za-z]+|\$?\d+%?$"""
    with open(file_name, encoding='utf-8') as f:
        r = re.findall(pattern, f.read())
        pass
    return Counter(r)


def word_count(file_path):
    """统计同一路径下所有文本文件中单词出现的次数"""
    os.chdir(file_path)
    counter = Counter()
    for i in os.listdir(os.getcwd()):
        if os.path.splitext(i)[1] == '.py':
            counter += calc_counter(i)
            pass
        pass
    for i in file_path:
        counter[i] = 0
        pass
    print(counter.most_common())
    pass


if __name__ == '__main__':
    word_count('../../py_language')
    pass